<template>
  <section>
    <el-dialog title="编辑" :visible.sync="visible" :close-on-click-modal="false" width="40%">
      <el-form label-width="120px" ref="form" :model="form" :rules="dataRule">
      <#list table.columnList as item >
          <#if item.name=="createId" ||  item.name=="createTime"
            || item.name=="updateTime">
              <#break>
          </#if>
          <#if item.type=="LocalDate">
            <el-form-item label="${item.comment}" prop="${item.name}">
              <el-date-picker size="medium" style="width:150px;" value-format="yyyy-MM-dd"
                              v-model="form.${item.name}" type="date" placeholder="${item.comment}"></el-date-picker>
            </el-form-item>
          <#elseif item.type=="LocalDateTime">
            <el-form-item label="${item.comment}" prop="${item.name}">
              <el-date-picker size="medium" style="width:150px;" value-format="yyyy-MM-dd HH:mm"
                              v-model="form.${item.name}" type="date" placeholder="${item.comment}"></el-date-picker>
            </el-form-item>
          <#elseif item.type=="Boolean">
            <el-form-item label="${item.comment}" prop="${item.name}">
              <el-switch
                  v-model="form.${item.name}"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="1"
                  :inactive-value="0">
              </el-switch>
            </el-form-item>
          <#elseif item.name!="createId" &&  item.name!="createTime"
          && item.name!="updateTime">
            <el-form-item label="${item.comment}" prop="${item.name}">
              <el-input v-model="form.${item.name}" placeholder="${item.comment}"></el-input>
            </el-form-item>
          </#if>

      </#list>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="visible = false">取消</el-button>
        <el-button type="primary" @click.native="submit" :loading="loading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
  export default {
    data() {
      return {
        visible: false,
        loading: false,
        form: {
    <#list table.columnList as item >
    ${item.name}:  '',
    </#list>
    },
      dataRule: {
      <#list table.columnList as item >
      ${item.name}:
        [{
          required: true,
          message: '${item.comment}不能为空',
          trigger: 'blur'
        }],
      </#list>

      }
    }
    },
    methods: {
      init(formData) {
        // this.$refs['form'].reset()
        this.visible = true
        this.$nextTick(() = > {
          this.$refs['form'].resetFields()
        if (formData) {
          this.form = formData
        } else {
          this.form = {}
        }
      })

      },
      submit() {
        this.$refs['form'].validate((valid) = > {
          if(
        !valid
      )
        {
          console.log('error submit!!')
          return false
        }
      else
        {
          this.loading = true
          this.$http.post('/api/${table.camelName}', this.form)
              .then((data) = > {
            this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1000,
            onClose: () = > {
            this.loading = false;
          this.visible = false
          this.$emit('refreshDataList')
        }
        })
        })
        .
          catch(e = > {
            this.loading = false;
          console.log(e)
          this.$message.error(e)
        })
        }
      })
        ;
      }
    }
  }
</script>